﻿@{
    ViewBag.Title = "我的订单";
    Layout = "~/Areas/Mall/Views/Shared/MallLayout1.cshtml";
    var orderstatus = ViewData["status"].ToString().Split(',');
}

<div class="order_t">
    <div sid="1" style="border-bottom: 3px red solid;" ck="1">
        <img src="/Images/o1.png" />
        <p>待付款(@orderstatus[0])</p>
    </div>
    <div sid="2">
        <img src="/Images/o2.png" /><br />
        <p>待发货(@orderstatus[1])</p>
    </div>
    <div sid="3">
        <img src="/Images/o3.png" />
        <p>待收货(@orderstatus[2])</p>
    </div>
    <div sid="4">
        <img src="/Images/o4.png" />
        <p>已完成(@orderstatus[3])</p>
    </div>
    <div sid="5">
        <img src="/Images/o5.png" />
        <p>退换货(@orderstatus[4])</p>
    </div>
</div>

<div style="width: 100%;height: auto;overflow: hidden;" id="orderlist">

</div>


<div style="width: 100%;height: 100px;float: left;margin-top: 20px;"></div>

<script>
    var pageCount = 5;
    var pageIndex = 0;
    $(window).scroll(function () {
        if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
            pageCount += 1;
            getlist();
        }
    });
    var sid = "@ViewData["sid"]";
    $(function () {

        $(".order_t div").each(function () {
            if ($(this).attr("sid") == sid) {
                $(".order_t div").css("border-bottom", "3px #cccccc solid");
                $(".order_t div").attr("ck", "0");
                $(this).css("border-bottom", "3px red solid");
                $(this).attr("ck", "1");
            }
        });
        getlist();
    });


    $(".order_t div").click(function () {
        $(".order_t div").css("border-bottom", "3px #cccccc solid");
        $(".order_t div").attr("ck", "0");
        $(this).css("border-bottom", "3px red solid");
        $(this).attr("ck", "1");
        getlist();
    });

    function getlist() {

        $(".order_t div").each(function () {
            if ($(this).attr("ck") == 1) {
                sid = $(this).attr("sid");
            }
        });

        $.post("@Url.Action("GetOrder")", { sid: sid, pageIndex: pageIndex * pageCount, pageCount: pageCount }, function (data) {
            var obj = JSON.parse(data);
            var count = obj.count[0].count;
            var gcount = 0;
            var gprice = 0;
            var str = "";
            for (var i = 0; i < obj.data.length; i++) {
                var item = obj.data[i];
                str += "<div class=\"order_list\">" +
                    "<div class=\"order_info\">" +
                    "<label style=\"float: left;\">订单号：" + item.OrderNo + "</label>" +
                    "<label style=\"float: right;\">时间：" + (item.Date.substring(0, 10)) + "</label>" +
                    "</div>";

                for (var j = 0; j < obj.data1.length; j++) {
                    var item1 = obj.data1[j];
                    if (item.Id == item1.OId) {
                        gcount += parseFloat(item1.Count);
                        gprice += parseFloat(item1.Price);

                        str += "<div class=\"order_g\">" +
                            "<img src=\"" + item1.Img1 + "\" />" +
                            "<div>" +
                            "<p>" + item1.GoodsName + "</p>" +
                            "<p>" + item1.Attr + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

                        if (item.Status == "4") {
                            str += "<label style=\"\" class=\"pjan\" onclick=\"pingjia('" + item1.GId + "','" + item.OrderNo + "')\">评价</label>";
                        }
                        str += "</p>" +
                            "<p>数量：" + item1.Count + "</p>" +
                            "<p class=\"orderxj\">小计：" + (parseFloat(item1.Price) * parseFloat(item1.Count)) + "元</p>" +
                            "</div>" +
                            "</div>";
                    }
                }
                str += "<div class=\"ordert_j\">" +
                    "<p style=\"text-align: right;\">共" + gcount + "件商品 合计：￥" + parseFloat(item.TotalPrice) + "元(含运费￥" + item.Freight + ")</p>" +
                    "</div>";

                str += "<div class=\"order_fk\">";
                if (item.Status == "2") {
                    //str += "<div class=\"order_fk_op\">提醒发货</div>";
                } else if (item.Status == "3") {
                    str += "<label style=\"float:left;\">物流：" + item.ExpressName + "</br>单号：" + item.WlNo + "</label><div class=\"order_fk_op\" onclick=\"shouhuo('" + item.OrderNo + "')\">确认收货</div>";
                } else if (item.Status == "4") {
                    str += "<div class=\"order_fk_op\" onclick=\"tuihuo('" + item.OrderNo + "')\">退货</div>";
                } else if (item.Status == "5") {
                    str += "<label style=\"float:left;color:red;\">正在处理您的退货请求！</label>";
                } else {
                    str += "<div class=\"order_fk_op\" onclick=\"pay('" + item.OrderNo + "','" + item.IsDc + "')\">付款</div>" +
                        "<div class=\"exitorder\" onclick=\"delorder('" + item.OrderNo + "')\">取消订单</div>";
                }

                str += "</div>" +
                    "</div>";
            }

            $("#orderlist").html(str);
        });
    }

    function delorder(oid) {
        if (confirm("你确定取消该订单吗？")) {
            $.post("@Url.Action("DelOrder")", { oid: oid }, function (row) {
                if (row > 0) {
                    location.reload();
                } else {
                    alert("删除失败！");
                }
            });
        }
    }

    function pay(orderno, dc) {
        if (dc == 1) {
            location.href = "@Url.Action("StartPay")?orderno=" + orderno;
        } else {
            location.href = "@Url.Action("Pay")?orderno=" + orderno;
        }
    }

    function pingjia(gid, orderno) {

        $.post("@Url.Action("CPj")", { id: gid, orderno: orderno }, function (row) {
            if (row > 0) {
                alert("该商品已经评价，请不要重复评价！");
                return false;
            } else {
                location.href = "@Url.Action("Evaluate")" + "?id=" + gid + "&orderno=" + orderno;
            }
        });

    }

    function tuihuo(orderno) {
        if (confirm("您确定要退货吗？")) {
            $.post("@Url.Action("EditOrderStatus")", { orderid: orderno, value: 5 }, function (data) {
                getlist();
            });
        }
    }

    function shouhuo(orderno) {
        if (confirm("您确定收货吗？")) {
            $.post("@Url.Action("ShouHuo")", { orderno: orderno }, function (row) {
                if (row > 0) {
                    location.href = "/mall/order/orderlist?sid=4";
                } else {
                    alert("操作失败！");
                }

            });
        }
    }
</script>